package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.BorrowListDao;
import person.aoao.library.dao.SeatListDao;
import person.aoao.library.dao.TypeseatListDao;
import person.aoao.library.dao.WillListDao;
import person.aoao.library.entity.SeatList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.SeatListService;
import person.aoao.library.vo.HomeData;
import person.aoao.library.vo.ListData;
import person.aoao.library.vo.SeatListPlus;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (SeatList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@Service("seatListService")
public class SeatListServiceImpl implements SeatListService {
    @Resource
    private SeatListDao seatListDao;

    @Resource
    private BorrowListDao borrowListDao;

    @Resource
    private WillListDao willListDao;

    @Resource
    private TypeseatListDao typeseatListDao;



    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SeatList queryById(Integer id) {
        System.out.println("seat"+id);
        SeatList item=this.seatListDao.queryById(id);
        item.setTypeseatList(this.typeseatListDao.queryById(item.getSeattype()));
        return item;
    }

    /**
     * 分页查询
     *
     * @param seatList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SeatList> queryByPage(SeatList seatList, PageRequest pageRequest) {
        long total = this.seatListDao.count(seatList);
        return new PageImpl<>(this.seatListDao.queryAllByLimit(seatList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param seatList 实例对象
     * @return 实例对象
     */
    @Override
    public SeatList insert(SeatList seatList) {
        this.seatListDao.insert(seatList);
        return seatList;
    }

    /**
     * 修改数据
     *
     * @param seatList 实例对象
     * @return 实例对象
     */
    @Override
    public SeatList update(SeatList seatList) {
        this.seatListDao.update(seatList);
        return this.queryById(seatList.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.seatListDao.deleteById(id) > 0;
    }

    @Override
    public List<SeatList> getSeatList(Integer fid) {
        List<SeatList> list =this.seatListDao.getSeatList(fid);
        for(SeatList item:list){
            System.out.println(item.getSeattype());
            item.setTypeseatList(this.typeseatListDao.queryById(item.getSeattype()));
        }
        return list;
    }

    @Override
    public ResponseData getSeatListAll(Integer num, Integer page) {
        ListData data=new ListData();
        List<SeatList> list=this.seatListDao.getSeatListAll(num,(page-1)*num);
        for(SeatList item:list){
            item.setTypeseatList(this.typeseatListDao.queryById(item.getSeattype()));
        }
        long count =this.seatListDao.getCount();
        data.setData(list);
        data.setCount(count);
        return  new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData searchSeatList(String str, Integer fid, Integer state, Integer seattype, Integer num, Integer page) {
        ListData data=new ListData();
        System.out.println(str);
        List<SeatList> list=this.seatListDao.searchSeatList("%"+str+"%",fid,state,seattype,num,(page-1)*num);
        for(SeatList item:list){
            item.setTypeseatList(this.typeseatListDao.queryById(item.getSeattype()));
        }
        long count=this.seatListDao.searchCount("%"+str+"%",fid,state,seattype);
        data.setData(list);
        data.setCount(count);
        return  new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getSeatListPlus(SeatListPlus qurey) {
        System.out.println(qurey.getRetruntime()+" 08:00:00");
        List<SeatList> list=this.seatListDao.getSeatList(qurey.getFid());
        for(SeatList seat:list){
            System.out.println(seat.getSeatid());
            seat.setTypeseatList(this.typeseatListDao.queryById(seat.getSeattype()));
            long num=this.willListDao.getSeatListPlus(seat.getId(),qurey.getRetruntime()+" 08:00:00");
            System.out.println(num);
            if(num>=3){
                seat.setUse(false);
            }else{
                seat.setUse(true);
            }
        }
        return  new ResponseData(ResponseCode.SUCCESS,list);
    }

    @Override
    public ResponseData getToDayList(String date) {
        ArrayList<HomeData> home=new ArrayList<>();
        String titles[]=new String[]{"今日借阅总数","今日归还总数","今日座位预约总数","今日座位使用次数"};
        long borrCount=this.borrowListDao.getBorrCountByDate(date+"%");
        long returnCount=this.borrowListDao.getReturnCountByDate(date+"%");
        long willCount=this.willListDao.getWillCountByDate(date+"%");
        long willUseCount=this.willListDao.getWillCountUse(date+"%");
        long counts[]=new long[]{borrCount,returnCount,willCount,willUseCount};
        for(int i=0;i<titles.length;i++){
            HomeData item=new HomeData();
            item.setTitle(titles[i]);
            item.setCount(counts[i]);
            home.add(i,item);
        }
        return new ResponseData(ResponseCode.SUCCESS,home);

    }
}
